/*
 * @Date: 2024-03-27 16:56:33
 * @LastEditors: bingkui.wu
 * @LastEditTime: 2024-04-07 09:36:53
 * @Description: 数据库链接对象
 */
import { log } from '@/common/utils';
import * as  mongoose from 'mongoose';

mongoose.set('strictQuery', false);

type TInput = {
    db: string;
};

export default ({ db }: TInput) => {

    const connect = () => {
        mongoose
            .connect(
                db,
            )
            .then(() => {
                return log(`mongodb connected to ${db}`);
            })
            .catch(error => {
                console.error('数据库链接失败，重新连接: ', error);
                setTimeout(connect, 5000);
            });
    };
    connect();

    mongoose.connection.on('disconnected', connect);
};
